﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace 添加文件版权
{
    public partial class MainFrm : Form
    {
        public MainFrm()
        {
            InitializeComponent();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            // 浏览...
            FolderBrowserDialog fdlog = new FolderBrowserDialog();
            if (fdlog.ShowDialog() == DialogResult.OK)
                this.textBox2.Text = fdlog.SelectedPath;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //开始批量加入
            if (this.textBox2.Text.Trim() == "")
                return;
            if (MessageBox.Show("给文件添加版权之前，请先备份！！！是否继续！！！", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                this.RansackFile(this.textBox2.Text);
                this.sRansackLog = "";
                MessageBox.Show("添加成功！");
            }
        }
        #region
        void RansackFile(string sPath)
        {
            //遍历当前文件夹下面的所有文件
            foreach (string sFile in Directory.GetFiles(sPath))
                this.sRansackLog += this.AddTextToFile(sFile, this.textBox1.Text);
            //遍历当前文件夹下面的所有文件夹
            foreach (string sDir in Directory.GetDirectories(sPath))
                this.RansackFile(sDir);
        }
        //添加文件版权信息，并返回错误日志报告
        string AddTextToFile(string sFile, string sText)
        {
            if (sFile.LastIndexOf(".csproj.FileList.txt") != -1 || sFile.LastIndexOf(".csproj.FileListAbsolute.txt") != -1)
                return "<font color=gray>.csproj.FileList.txt类型的文件是VS文件，不能修改</font>";

            string Exten = Path.GetExtension(sFile).ToLower();

            //格式化版权信息
            switch (Exten)
            {
                // case ".cs": sText = string.Format("\n\t/// <summary>\n\t/// {0}\n\t/// <summary>\n", sText.Replace("\n", "\n\t/// ")); break;
                case ".cs": break;
                case ".vb": sText = string.Format("\n\t/// <summary>\n\t/// {0}\n\t/// <summary>\n", sText.Replace("\n", "\n\t/// ")); break;
                case ".html": sText = string.Format("<!--\n{0}\n//-->\n", sText); break;
                case ".htm": sText = string.Format("<!--\n{0}\n//-->\n", sText); break;
                case ".aspx": sText = string.Format("<!--\n{0}\n//-->\n", sText); break;
                case ".asp": sText = string.Format("<!--\n{0}\n//-->\n", sText); break;
                case ".js": sText = string.Format("//{0}\n", sText); break;
                case ".txt": sText = string.Format("{0}\n", sText); break;
                default: return "文件 [ " + sFile + " ] <font color=red>不在指定文件类型范围内</font>\n";//否则退出方法，不进行文件版权信息添加
            }

            StreamReader sr = new StreamReader(sFile, Encoding.Default);
            string rText = sr.ReadToEnd();
            sr.Close();

            //如果文件中已经有了这个版权就不添加了
            if (rText.IndexOf(sText) != -1)
                return "文件 [ " + sFile + " ] <font color=blue>已经存在此权版</font>\n";

            if (rText.Length > 1 && sText.Length > 1)
            {
                int AddIndex = rText.IndexOf("u");
                StreamWriter sw = new StreamWriter(sFile, false, Encoding.UTF8);
                if (AddIndex == 0)
                {
                    sw.Write(rText.Substring(0, AddIndex));
                }
                else
                {
                    sw.Write(rText.Substring(0, AddIndex + 1));
                }
                sw.Write(sText);
                if (AddIndex == 0)
                {
                    sw.Write(rText.Substring(AddIndex));
                }
                else
                {
                    sw.Write(rText.Substring(AddIndex + 1));
                }
                sw.Close();
            }
            for (int i = 0; i < this.progressBar1.Maximum; i++)
                this.progressBar1.Value = i;
            this.progressBar1.Value = 0;
            return "文件 [ " + sFile + " ] <font color=DarkOrange>OK</font>\n";
        }
        string sRansackLog = "";//文件之版权添加日志记录AddCopyRightLog
        #endregion
        private void MainFrm_Load(object sender, EventArgs e)
        {
            this.label2.Text = "    .CS类型的文件自动在namespace下面加上上面的说明性(如:版权)文字\n\n支持文件类型：.cs  .vb  .html  .htm  .aspx  .asp  .js  .txt";

            StringBuilder list = new StringBuilder();
            list.AppendLine("/*******************************************************");
            list.AppendLine("Author: 罗敏贵");
            list.AppendLine("Explain：");
            list.AppendLine("Versions: V 1.0 版");
            list.AppendLine("E-mail: minguiluo@163.com");
            list.AppendLine("Blogs： http://www.cnblogs.com/luomingui");
            list.AppendLine("History:");
            list.AppendLine("      CreateDate " + DateTime.Now.ToString() + "");
            list.AppendLine("    ");
            list.AppendLine("*******************************************************/");

            this.textBox1.Text = list.ToString();
        }
    }
}
